using System.Collections.Generic;
using System.Threading.Tasks;
using JoyfulHomeJourney.BackEnd.Infrastructure.Atrributes;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Requests.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses;
using JoyfulHomeJourney.BackEnd.Infrastructure.Dtos.Responses.Objects;
using JoyfulHomeJourney.BackEnd.Infrastructure.EFCore;
using JoyfulHomeJourney.BackEnd.Infrastructure.Tools;
using JoyfulHomeJourney.BackEnd.Server.EFCore;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Requests;
using JoyfulHomeJourney.BackEnd.Server.Instances.Extends.Responses;
using JoyfulHomeJourney.BackEnd.Server.Interfaces;
using Microsoft.EntityFrameworkCore;
namespace JoyfulHomeJourney.BackEnd.Server.Instances
{
/// <summary>
/// 一个类包含了服务类所需的所有仓库表的实例，仅提供给该服务类使用，请将所有服务类所需的仓库表的实例都放在该类中。
/// 仓库表的实例指提供了操作数据库指定表的所有方法的对象
/// </summary>
	internal class DiscountsEF
	{
		public DiscountsEF(DbContext dbContext)
		{
			discountsServerEF = new DiscountsServerEF(dbContext);
            vipDiscountsServerEF=new VipDiscountsServerEF(dbContext);
		}
		public DiscountsServerEF discountsServerEF { get; set; }
        public VipDiscountsServerEF vipDiscountsServerEF { get; set; }
	}
	[InjectClass]
	public class DiscountsServer : IDiscountsServer
	{
		private readonly DiscountsEF _discountsEF;//包含所有所需仓库表实现的对象
		// 请不要修改该构造方法,也无需添加属性，而应该在上面的EF类中添加属性和构造函数赋值语句
		public DiscountsServer(DbContext dbContext)
		{
			_discountsEF = new DiscountsEF(dbContext);
		}
        public async Task<ResponseApiDto> AddAsync(RequestDiscountsDto dto)
        {
            return ResponseApiDto.Create(await _discountsEF.discountsServerEF.AddAsync(dto.ToCreateEntity()));
        }
        public async Task<ResponseApiDto> DeleteAsync(RequestDiscountsDto dto)
        {
            return ResponseApiDto.Create(await _discountsEF.discountsServerEF.DeleteAsync(dto.ToDeleteEntity()));
        }
        public async Task<ResponseApiDto> UpdateAsync(RequestDiscountsDto dto)
        {
            return ResponseApiDto.Create(await _discountsEF.discountsServerEF.UpdateAllProperNotNullAsync(dto.ToUpdateEntity()));
        }
        public async Task<ResponseApiDto> PaginationAsync(int page, int limit)
        {
            int total = 0;
            List<ResponseDiscountsDto> data = new List<ResponseDiscountsDto>();
            List<Discounts> discountss = await _discountsEF.discountsServerEF.Pagination1(d => true, false, d => d.DCreateTime, out total, page, limit).ToListAsync();
            foreach (var discounts in discountss)
            {
                data.Add(new ResponseDiscountsDto
                {
                    count = await _discountsEF.vipDiscountsServerEF.Query(d => d.VdDiscountId == discounts.DId).CountAsync(),
                    id = discounts.DId,
                    discount = discounts.DDiscount,
                    maxDiscountCount = discounts.DMaxDiscountCount,
                    createTime = discounts.DCreateTime,
                });
            }
            foreach (var item in data)
            {
                item.Time = Tools.TimeToString(item.createTime);
            }
            return ResponseApiDto.Success("数据已获取", new ResponsePaginationDto { total = total, data = data });
        }
    }
}
